<?

// what php version are we using?
{ $version = explode( '.', phpversion() );
	define( 'IS_PHP4', ($version[0]<5) ); }

/**
 *  this function handles loading of smutty classes as and
 *  when they're needed, but is only supported by php5 >
 *
 *  @param [className] the name of the class to load
 *
 */

function __autoload( $className ) {

	// first try the library
	$path = preg_replace( '/_/', '/', $className );
	$path = "library/$path.class.php";
	if ( file_exists($path) ) {
		include $path;
		return true;
	}

	// then maybe a model?
	$path = "application/models/$className.php";
	if ( file_exists($path) ) {
		// this data gets added to the class dynamically.
		// need this cause of php's static lmitation thing...
		// this is a ***VERY*** dirty hack, but it works and i
		// like the syntax it allows so it's stayin for now!
		$replace = "

function _getFields() {
	return parent::_getFields( '$className' );
}

function find( \$id, \$name = 'id' ) {
	return parent::find( '$className', \$id, \$name );
}

function fetchAll( \$order = false, \$params = array(), \$limit = false ) {
	return parent::fetchAll( '$className', \$order, \$params, \$limit );
}

";
		// read in model class data to alter then eval
		$data = file_get_contents( $path );
		$data = preg_replace( '/<\?/', '', $data );
		$data = preg_replace( '/\?>/', '', $data );
		$data = preg_replace( '/extends Smutty_Model {/', "extends Smutty_Model { $replace ", $data );
		eval( $data );
		if ( IS_PHP4 )
			overload( $className );
		return true;
	}

	die( 'Required class file not found:' . $className );

}

class Smutty_Main {

	/**
	 *  use this to load one or more classes on demand.  php5 can
	 *  use the __autoload() function itself to have classes loaded,
	 *  but previous versions will need to load classes before
	 *  they are accessed.
	 *
	 *  @param [classes] array/string of class names
	 *
	 */

	function loadClass() {

		// php5 doesn't need this method
		if ( !IS_PHP4) return;

		static $loaded;
		if ( $loaded == null )
			$loaded = array();

		for ( $i=0; $i<func_num_args(); $i++ ) {
			$class = func_get_arg( $i );
			if ( !$loaded[$class] ) {
				$loaded[ $class ] = true;
				__autoload( $class );
			}
		}

	}

	/**
	 *  this is the entry point of the application, call this
	 *  to get everything going!!!
	 *
	 */

	function run() {

		// PHP setup
		Smutty_Main::loadClass(
			'Smutty_Exception', 'Smutty_Utils',
			'Smutty_Config', 'Smutty_Data'
		);
		ini_set( 'session.cookie_path', Smutty_Utils::getBaseUrl() . '/' );

		// set correct language
		$cfg = Smutty_Config::getInstance();
		$lang = $cfg->get('smutty.lang') ? $cfg->get('smutty.lang') : 'en';
			include "library/lang/$lang.php";

		// first check the resource to see if it's a public resource
		// that the user has requested.
		$resource = Smutty_Utils::getSmuttyUrl();
		if ( substr($resource,0,6) != 'smutty' ) {
			$path = "application/public/$resource";
			if ( $resource && file_exists($path) ) {
				Smutty_Main::loadClass( 'Smutty_Resource' );
				Smutty_Resource::output( $path );
			}
		}

		// load the required classes now
		Smutty_Main::loadRequiredClasses();
		// then create the router and dispatch the request
		$router = Smutty_Router::getInstance();
		$router->dispatch();

		Smutty_Main::completeRequest();

	}

	/**
	 *  this function does any cleanup/saving needed
	 *  before the request is done.
	 *
	 */

	function completeRequest() {

		// request complete, save the session data
		$session = Smutty_Session::getInstance();
		$session->save();

	}

	/**
	 *  this function makes sure all the classes needed to
	 *  run smutty are loaded and readyf or use.  with PHP5
	 *  these will be autoloaded on demand, but for PHP4
	 *  we have to load them at the start.
	 *
	 */

	function loadRequiredClasses() {

		// the Model we use is PHP4/5 dependent
		Smutty_Main::loadClass( 'Smutty_Model_Base' );
		$modelVer = IS_PHP4 ? 'PHP4' : 'PHP5';
		include "library/Smutty/Model/$modelVer.class.php";

		// first load the Smutty classes we're gonna need
		Smutty_Main::loadClass(
			'Smutty_Exception', 'Smutty_Utils',
			'Smutty_Database', 'Smutty_Config',
			'Smutty_Template', 'Smutty_Controller',
			'Smutty_Router', 'Smutty_Data',
			'Smutty_Template_Smutty', 'Smutty_Session'
		);

		if ( IS_PHP4 ) {
			// then load the users models so they're available
			$d = opendir( 'application/models/' );
			while ( $f = readdir($d) )
				if ( preg_match('/(.*)\.php$/',$f,$matches) )
					Smutty_Main::loadClass( $matches[1] );
			closedir( $d );
		}

	}

}

?>